package huaweiod.first;

/**
 * 华为机试真题 Java 实现【箱子之字形摆放】【2022.11 Q4 新题】
 */
public class A_2023_35_BoxFontPlacement {

    public static void boxFontPlacement(String str, int n) {

        if (str == null || str.length() == 0 || n <= 0) {
            return;
        }
        int len = str.length();
        int col = len / n + (len % n == 0 ? 0 : 1);
        char[][] arr = new char[n][col];
        boolean flag = true;
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            if (flag) {
                for (int j = 0; j < col; j++) {
                    if (index >= len) break;
                    arr[j][i] = str.charAt(index++);
                }
            } else {
                for (int j = col - 1; j >= 0; j--) {
                    if (index >= len) break;
                    arr[j][i] = str.charAt(index++);
                }
            }
            flag = !flag;
        }
        for (int i = 0; i < arr.length; i++) {
            StringBuilder s = new StringBuilder();
            for (int j = 0; j < arr[i].length; j++) {
                s.append(arr[i][j]);
            }
            System.out.println(s);
        }
    }

    public static void main(String[] args) {
        boxFontPlacement("ABCDEFG", 3);
    }
}
